import operator
import time
from osv import osv, fields
from osv.orm import intersect
import tools.sql
from tools import config
from tools.translate import _
from mx import DateTime
import netsvc

class kdvn_accounting_payment_supplier(osv.osv):
    _name = 'kdvn.accounting.payment.supplier'
    _description = 'KDVN Accounting Payment Supplier'
    _auto = False
    _columns={
             'payment_id':fields.many2one('kdvn.request.of.payment','ROP No.'),
             'vat_id':fields.many2one('kdvn.purchase.red.invoice','VAT No.'),
             'supplier_id':fields.many2one('res.partner','Supplier'),
             'date_paid':fields.date('Date'),
             'currency_id':fields.many2one('kdvn.purchase.currency','Curr.'),
             'paid_amount':fields.float('Paid Amount'),
             'equivalent_vnd':fields.float('Equivalent VND')
             }
    def init(self,cr):
        cr.execute("Create or replace view kdvn_accounting_payment_supplier as Select \
                        (krop.id::text || kdvn_purchase_red_invoice_id::text)::bigint as id,\
                        krop.id as payment_id,\
                        kpri.id as vat_id,\
                        supplier_id,\
                        kp.date as date_paid,\
                        kpri.currency_id,\
                        kpri.total_amount as paid_amount,\
                        kpri.equivalent_vnd\
                    from\
                        kdvn_request_of_payment krop\
                    left join\
                        (Select payment_id,date from kdvn_payment group by payment_id,date) kp on krop.id = kp.payment_id\
                    left join\
                        res_partner rp on supplier_id = rp.id\
                    left join\
                        kdvn_request_of_payment_kdvn_purchase_red_invoice_rel krvv on krop.id = rop_id\
                    left join\
                        kdvn_purchase_red_invoice kpri on kdvn_purchase_red_invoice_id=kpri.id\
                    where\
                        krop.state='paid' and coalesce(kp.payment_id,0)>0 order by paymentno")
kdvn_accounting_payment_supplier()